﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;

namespace APMAWebConsole.ReportingPart
{
    public partial class Burnup : System.Web.UI.Page
    {
        private const string IterationsListKey = "IterationsListKey";
        private const string SelectedValueKey = "SelectedValueKey";

        public DataTable IterationsList
        {
            get
            {
                return ViewState[IterationsListKey] != null
                           ? (DataTable)ViewState[IterationsListKey]
                           : (DataTable)(ViewState[IterationsListKey] = SiteMaster.Proxy.GetIterationsList());
            }
        }

        public string SelectedValue
        {
            get
            {
                return Session[SelectedValueKey] != null
                           ? Session[SelectedValueKey].ToString()
                           : null;
            }
            set { Session[SelectedValueKey] = value; }
        }

        public DataTable BurnUpDataSource()
        {
            int parsedValue = 0;
            bool isParsed = false;
            if (SelectedValue != null)
                isParsed = int.TryParse(SelectedValue, out parsedValue);

            return SiteMaster.Proxy.GetBurnUpDownChart(isParsed ? parsedValue : 0);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.User.Identity.IsAuthenticated)
                Response.Redirect("~/Account/Login.aspx");

            SelectedValue = null;
        }

        private void Rebind()
        {
            ddlUserStoryIterationId.Items.Clear();
            ddlUserStoryIterationId.DataValueField = "Id";
            ddlUserStoryIterationId.DataTextField = "Id";
            ddlUserStoryIterationId.DataSource = IterationsList;
            ddlUserStoryIterationId.DataBind();
            ddlUserStoryIterationId.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            ddlUserStoryIterationId.SelectedValue = SelectedValue;
        }

        protected void btnUserStoryChoose_OnClick(object sender, EventArgs e)
        {
            SelectedValue = ddlUserStoryIterationId.SelectedValue;

            Chart1.DataBind();
            updChart.Update();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            Rebind();
        }

        protected void Chart1_Load(object sender, EventArgs e)
        {

        }

    }
}